/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Principal.java
 *
 * Created on 26-oct-2010, 13:39:30
 */

package app;

import com.digitalpersona.onetouch.DPFPTemplate;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import enroll.EnrollmentForm;
import enroll.VerificationForm;
import java.awt.Component;
import java.awt.Image;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.sql.SQLException;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

/**
 *
 * @author null
 */
public class Principal extends javax.swing.JFrame {

    //atributos
    public JDialog controlLogin;
    public static String TEMPLATE_PROPERTY = "template";
    private static DPFPTemplate template;
    EnrollmentForm enrollForm = null;
    RegistroHuella registroHuella = null;
    AboutApp about = null;
    
    /** Creates new form Principal */
    public Principal() {
        initComponents();
        Image icono = new ImageIcon(getClass().getResource("/images/icono.png")).getImage();
        this.setIconImage(icono);
        this.setLocationRelativeTo(null);
        this.setExtendedState(MAXIMIZED_BOTH);
        
        //para recibir la confirmacion ...de la huella creada ya...
        this.addPropertyChangeListener(TEMPLATE_PROPERTY, new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                //verify.setEnabled(template != null);
                //save.setEnabled(template != null);
                if (evt.getNewValue() == evt.getOldValue()) return;
                if (template != null){
                    int confirm = JOptionPane.showConfirmDialog (Principal.this, "La huella ha sido capturada, desea proceder con la registracion de la misma?", "Capturador de huellas", JOptionPane.YES_NO_OPTION /*INFORMATION_MESSAGE*/);                                        
                    if(confirm == 0){                        
                        //aqui conectar con el proceso de registracion...
                        registroHuella = new RegistroHuella(Principal.this, rootPaneCheckingEnabled);
                        registroHuella.setVisible(true);        //abrimos la interfaz de registro
                    }else{
                        enrollForm.dispose();               //cerramos la interfaz                                             
                    }
                }
            }
        });        
    }

    /**
     * Metodo para salir de la aplicacion
     */
    public void salir(){
        int i = JOptionPane.showConfirmDialog(this,"<html><center><font size = 3 color = black>¿Realmente Desea Salir?","Confirmar",JOptionPane.YES_NO_OPTION);
        if(i == 0){
            System.exit(0);
        }
    }

     /*
     * Para manejar la lectura de la huella (desde el objeto en memoria)
     */
    public static DPFPTemplate getTemplate() {
        return template;
    }
    /**
     * Para obtener el template leido desde un .fpt
     * @param template
     */
    public void setTemplate(DPFPTemplate template) {
        DPFPTemplate old = Principal.template;
        Principal.template = template;
        firePropertyChange(TEMPLATE_PROPERTY, old, template);
    }

    /*funciones anhadidas por netbeans*/
    /*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        MenuAccion1 = new javax.swing.JMenuItem();
        MenuAccion2 = new javax.swing.JMenuItem();
        MenuSalir = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Enrollment");
        setBackground(new java.awt.Color(255, 255, 255));
        setForeground(new java.awt.Color(255, 255, 255));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 727, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 544, Short.MAX_VALUE)
        );

        jMenuBar1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jMenu1.setText("Menu");
        jMenu1.setFont(new java.awt.Font("Arial Black", 0, 14));
        jMenu1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu1ActionPerformed(evt);
            }
        });

        MenuAccion1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
        MenuAccion1.setFont(new java.awt.Font("Arial", 0, 14));
        MenuAccion1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/action.png"))); // NOI18N
        MenuAccion1.setText("Capturar huella");
        MenuAccion1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        MenuAccion1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MenuAccion1ActionPerformed(evt);
            }
        });
        jMenu1.add(MenuAccion1);

        MenuAccion2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_G, java.awt.event.InputEvent.CTRL_MASK));
        MenuAccion2.setFont(new java.awt.Font("Arial", 0, 14));
        MenuAccion2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/action.png"))); // NOI18N
        MenuAccion2.setText("Registrar huella");
        MenuAccion2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        MenuAccion2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MenuAccion2ActionPerformed(evt);
            }
        });
        jMenu1.add(MenuAccion2);

        MenuSalir.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
        MenuSalir.setFont(new java.awt.Font("Arial", 0, 14));
        MenuSalir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/exit.png"))); // NOI18N
        MenuSalir.setText("Salir");
        MenuSalir.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        MenuSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MenuSalirActionPerformed(evt);
            }
        });
        jMenu1.add(MenuSalir);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("About...");
        jMenu2.setFont(new java.awt.Font("Arial Black", 0, 14));
        jMenu2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu2ActionPerformed(evt);
            }
        });

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_D, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        jMenuItem1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/apply.png"))); // NOI18N
        jMenuItem1.setText("About...");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem1);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void MenuAccion1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MenuAccion1ActionPerformed

        enrollForm = new EnrollmentForm(this);
	enrollForm.setVisible(true);
}//GEN-LAST:event_MenuAccion1ActionPerformed

    private void MenuAccion2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MenuAccion2ActionPerformed
        
        RegistroHuella  registroHuella2 = new RegistroHuella(this, rootPaneCheckingEnabled);
        registroHuella2.setVisible(true);
}//GEN-LAST:event_MenuAccion2ActionPerformed

    private void MenuSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MenuSalirActionPerformed
      
        this.salir();
}//GEN-LAST:event_MenuSalirActionPerformed

    private void jMenu1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu1ActionPerformed

}//GEN-LAST:event_jMenu1ActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        this.salir();
    }//GEN-LAST:event_formWindowClosing

    private void jMenu2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu2ActionPerformed
        
    }//GEN-LAST:event_jMenu2ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
        about = new AboutApp(this, rootPaneCheckingEnabled);
        about.setVisible(true);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

       

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem MenuAccion1;
    private javax.swing.JMenuItem MenuAccion2;
    private javax.swing.JMenuItem MenuSalir;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

     /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        /*
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Principal().setVisible(true);
            }
        });
        */
         SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                 new Principal().setVisible(true);
            }
        });
    }
}
